package com.service.util;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;

import com.bpms.common.VO;

public class MD5DigitVerifyUtil {
	public static final String MD5_SIGN_NAME_VALUE="zk_1234";
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		VO vo=new VO();
		  vo.setAttr("Action", "device_list");
		  vo.setAttr("NetWorkCode", "");
		  vo.setAttr("t", "1351137058958");
		  vo.setAttr("s", "a3ee3e80103a153f9d3db4ccfd34b2e3");
		  vo.setAttr("r", "JSON");
		  vo.setAttr("BELONG_ORG_NAME", "151");
		  vo.setAttr("m", "device_list");
		  vo.setAttr("deviceip", "");
		  vo.setAttr("device_name", "");
		try {
		 boolean is_success=MD5DigitVerifyUtil.checkMD5DigitVerify(vo);
		 System.out.println("is_success="+is_success);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unchecked")
	public static boolean checkMD5DigitVerify(VO vo) throws Exception{
		boolean is_check_same=false;
		 try {
			Map<String,String> map=new TreeMap<String,String>();
			Map<String, String> ht=vo.attrs();
			Iterator ht_it=ht.entrySet().iterator();
			while(ht_it.hasNext()){
				Entry<String,String> en=(Entry) ht_it.next();
				String key=en.getKey().toString().toLowerCase();
				String value=en.getValue();
//				System.out.println("key="+key+",value="+value);
				map.put(key, value);
			}
//			  map.put("action", "device_list");
//			  map.put("networkcode", "");
//			  map.put("t", "1351137058958");
//			  map.put("s", "a3ee3e80103a153f9d3db4ccfd34b2e3");
//			  map.put("r", "JSON");
//			  map.put("belong_org_name", "151");
//			  map.put("m", "device_list");
//			  map.put("deviceip", "");
//			  map.put("device_name", "");
			  List<String> list=new ArrayList();
			  for(Iterator it=map.entrySet().iterator();it.hasNext();){
				  Map.Entry<String,String> entry=(Entry) it.next();
				  String key=entry.getKey();
				  String value=entry.getValue();
				  list.add(key);
			  }
			  String all_str="";
			  for (String object : list) {
				  String key=object.toString();
				  String value=map.get(key);
				  //System.out.println("key="+key+",value="+value);
				  if(key.equals("s")||key.equals("timestamp")||key.equals("image1")||key.equals("image2")||key.equals("image3")||key.equals("image4")){
					  continue;
				  }
				  all_str+=key+"="+value+"&";
			  }
			  System.out.println("====all_str="+all_str+MD5_SIGN_NAME_VALUE);
			  String md5_value=MD5Util.string2MD5(all_str+MD5_SIGN_NAME_VALUE);
			  String s=map.get("s");
			  System.out.println(md5_value);
			  System.out.println(s);
			  System.out.println(md5_value.equals(s));
			  is_check_same=md5_value.equals(s);
		} catch (Exception e) {
			e.printStackTrace();
		}
	    return is_check_same;
	}

}
